package net.borderwars.simulator.events;

import net.borderwars.robots.VirtualMachine;

/**
 * @author Eric
 *         Date: Mar 8, 2004
 *         Time: 9:05:03 PM
 */
public class CPUCycle extends BEvent {
    VirtualMachine vm = null;

    public CPUCycle(long l, VirtualMachine vm) {
        super(l);
        this.vm = vm;
    }

    public String getName() {
        return ("CPU Tick for " + vm);
    }

    public void process() {
        // vm.setNextEvent (null);
        int done = 1000;
        try {
            done = vm.tick();
        }
        catch (Exception e) {
            System.err.println("This robot has done something bad -- he shall not be rescheduled");
            e.printStackTrace();
        }
        if (done != VirtualMachine.FINISHED) {
            BEvent next = null;//= vm.getNextEvent ();
            if (next == null) { // If a new next event was not created by our instruction that was executed
                // then just reschedule the next cpu cycle for this robot
                this.timeStamp = new Long(this.timeStamp.longValue() + 10);
                eq.placeEvent(this);
            } else { // if the nextInstruction let the VM know about the next event... place that on the queue instead
                // an example of this would be.. when a drive command is issued.  A new event will be placed
                // on the queue for next time the robot must perform his collision checks

                eq.placeEvent(next);
            }
        }
    }
}
